package com.yjhb.android.sbz.manager;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.media.RingtoneManager;
import android.os.Build;

import androidx.core.app.NotificationCompat;

import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.activity.MsgNoticeActivity;

/**
 * 创建者 zx
 * 创建时间 2021/3/5 16:45
 */
public class MsgNotificationManager {

    public static final String CHANNEL_ID_CHAT = "chat";
    public static final String CHANNEL_ID_SYS = "system";
    private static NotificationManager manager;
    private static int id = 1;


    public static void sendNotification(Context context, int type, String title, String msg) {
        if (manager == null) {
            manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        }
        //判断是否为8.0以上：Build.VERSION_CODES.O为26
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //创建通知渠道ID
            String channelId = type == 1 ? CHANNEL_ID_CHAT : CHANNEL_ID_SYS;
            //创建通知渠道名称
            String channelName = type == 1 ? "用户消息" : "系统消息";
            //创建通知渠道重要性
            int importance = NotificationManager.IMPORTANCE_HIGH;
            createNotificationChannel(channelId, channelName, importance);
        }

        Intent intent = new Intent(context, MsgNoticeActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 1000, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        //创建Notification，传入Context和channelId
        Notification notification = new NotificationCompat.Builder(context, CHANNEL_ID_CHAT)
                .setAutoCancel(true)
                .setContentTitle(title)
                .setContentText(msg)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.logo)
                .setPriority(Notification.PRIORITY_MAX)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setVibrate(new long[]{100, 200, 100, 200})
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.logo))
                .setContentIntent(pendingIntent)
                .build();
        manager.notify(++id, notification);
    }


    //创建通知渠道
    @TargetApi(Build.VERSION_CODES.O)
    private static void createNotificationChannel(String channelId, String channelName, int importance) {
        NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
        channel.setShowBadge(true);
        channel.enableVibration(true);
        channel.setVibrationPattern(new long[]{100, 200, 100, 200});
        //设置声音
        channel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), Notification.AUDIO_ATTRIBUTES_DEFAULT);
        //为NotificationManager设置通知渠道
        manager.createNotificationChannel(channel);
    }

}
